#!/usr/local/bin/php
<?php
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';
require_once OPEN_LIB_PATH . '/open/models/OaAppConfigOrderModel.php';

$logger = Log::factory('console', '', '', array(
    'lineFormat' => '[%1$s] [%3$s] %5$s(%6$s): %4$s',
    'timeFormat' => '%Y-%m-%d %H:%M:%S'
), PEAR_LOG_INFO);
$logger->setPriority(PEAR_LOG_INFO);

$options = getopt('a:o:');
if (empty($options)) {
    $logger->err("usage: " . basename(__FILE__) . " -a app_id -o order_id");
    exit();
}
if (!isset($options['a'])) {
    $logger->err("no app id");
    exit();
}
$appId = (int) $options['a'];
if ($appId <= 0) {
    $logger->err("app id invalid: app-$appId");
    exit();
}
if (!isset($options['o'])) {
    $logger->err("no order id");
    exit();
}
$orderId = $options['o'];

$appModel = new OaAppModel(OaAppModel::APP_CONFIG_ENV_PRODUCTION, $logger);
$appConfigOrderModel = new OaAppConfigOrderModel($logger);
$app = $appModel->getApp($appId);
if (!$app) {
    $logger->err("app not exists: app-$appId");
    exit();
}
if ($appConfigOrderModel->createConfigOrder($orderId, $appId, $app['creator']) == false) {
    $logger->err("create config order failed: app-$appId order-$orderId");
} else {
    $logger->err("create config order succeed: app-$appId order-$orderId");
}
